﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.MessageServiceRef;
    using WMS6.Web.UserControls;

    public class SendMessageList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnGoBack;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F1228;
        protected GridView gvUserMessage;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUserSendMessage;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2RoleList(UserMessageQueryEntity userMessageQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(userMessageQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = MessageServiceClientProxy.GetInstance().GetUserMessageInfoByQuery(userMessageQuery);
            base.BindData2GridView(this.gvUserMessage, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = new List<string>();
                string redMessages = "";
                string messageStr = "";
                for (int i = 0; i < this.gvUserMessage.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvUserMessage.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        if (((Label) this.gvUserMessage.Rows[i].FindControl("lblIS_READ")).Text == "Y")
                        {
                            redMessages = redMessages + ((HyperLink) this.gvUserMessage.Rows[i].FindControl("hlMessageTitle")).Text + "/";
                        }
                        else
                        {
                            selectedID.Add(((Label) this.gvUserMessage.Rows[i].FindControl("lblRowID")).Text.ToString());
                            messageStr = messageStr + ((HyperLink) this.gvUserMessage.Rows[i].FindControl("hlMessageTitle")).Text + "/";
                        }
                    }
                }
                if (redMessages != "")
                {
                    this.lblMsg.Text = redMessages + base.GetGlobalMsgResource("F9230_015");
                }
                else if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
                }
                else if (int.Parse(MessageServiceClientProxy.GetInstance().DeleteSendMessage(selectedID)) > 0)
                {
                    this.lblMsg.Text = messageStr + base.GetGlobalMsgResource("F4100_012");
                    LicenseUtils.IsUnreadMessageChange = true;
                    UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
                    this.BindData2RoleList(userMessageQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvUserMessage, "ROWID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_006");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_007");
            }
            else
            {
                UserMessageInfo userMessageInfo = MessageServiceClientProxy.GetInstance().GetUserMessageInfoByQueryEntity(selectedID[0]);
                if ((userMessageInfo != null) && (userMessageInfo.IsRead == "Y"))
                {
                    this.lblMsg.Text = "该消息已被接收者查看，不能编辑。";
                }
                else
                {
                    base.Response.Redirect(string.Format("SendMessage.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            userMessageQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = userMessageQuery.CurrentPage;
            userMessageQuery.CurrentPage = "0";
            this.gvUserMessage.AllowSorting = false;
            this.BindData2RoleList(userMessageQuery);
            this.hdnActionID.Value = "";
            this.gvUserMessage.ShowFooter = false;
            userMessageQuery.IsGetAll = false;
            userMessageQuery.CurrentPage = currentPageIndex;
            this.SetUserMessageQueryEntity(userMessageQuery);
            base.Export2Excel("SendMessage.xls", this.gvUserMessage);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            userMessageQuery.CurrentPage = "0";
            this.BindData2RoleList(userMessageQuery);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("SendMessageQuery.aspx", false);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            userMessageQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2RoleList(userMessageQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            userMessageQuery.MessageTitle = "";
            userMessageQuery.SystemCreated = "='N'";
            userMessageQuery.IsRead = "";
            userMessageQuery.CreatedDate = "";
            userMessageQuery.ToUserID = "";
            userMessageQuery.FromUserID = "='" + base.UserInfo.UserID + "'";
            userMessageQuery.IsActive = "";
            this.BindData2RoleList(userMessageQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("SendMessageQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            if (int.Parse(userMessageQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                userMessageQuery.CurrentPage = (int.Parse(userMessageQuery.CurrentPage) + 1).ToString();
                this.BindData2RoleList(userMessageQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
            if (int.Parse(userMessageQuery.CurrentPage) > 0)
            {
                userMessageQuery.CurrentPage = (int.Parse(userMessageQuery.CurrentPage) - 1).ToString();
                this.BindData2RoleList(userMessageQuery);
            }
        }

        private UserMessageQueryEntity GetUserMessageQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as UserMessageQueryEntity);
            }
            return (base.PageQueryEntitySession as UserMessageQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
                userMessageQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2RoleList(userMessageQuery);
            }
        }

        protected void gvUserMessage_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1225", this.gvUserMessage);
        }

        protected void gvUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("hlMessageTitle")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvUserMessage_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                UserMessageQueryEntity userMessageQuery = this.GetUserMessageQueryEntity();
                userMessageQuery.SortField = e.SortExpression;
                if (userMessageQuery.SortDirection == "ASC")
                {
                    userMessageQuery.SortDirection = "DESC";
                }
                else if (userMessageQuery.SortDirection == "DESC")
                {
                    userMessageQuery.SortDirection = "ASC";
                }
                this.BindData2RoleList(userMessageQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            UserMessageQueryEntity userMessageQuery = new UserMessageQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    userMessageQuery.MessageTitle = this.PreviousPage.MessageTitle;
                    userMessageQuery.SystemCreated = "='N'";
                    userMessageQuery.IsRead = this.PreviousPage.IsRead;
                    userMessageQuery.CreatedDate = this.PreviousPage.CreatedDate;
                    userMessageQuery.ToUserID = this.PreviousPage.ToUserID;
                    userMessageQuery.FromUserID = "='" + base.UserInfo.UserID + "'";
                    userMessageQuery.IsActive = this.PreviousPage.IsActive;
                }
                else
                {
                    userMessageQuery.MessageTitle = "";
                    userMessageQuery.SystemCreated = "='N'";
                    userMessageQuery.IsRead = "";
                    userMessageQuery.CreatedDate = "";
                    userMessageQuery.ToUserID = "";
                    userMessageQuery.FromUserID = "='" + base.UserInfo.UserID + "'";
                    userMessageQuery.IsActive = this.PreviousPage.IsActive;
                }
            }
            else
            {
                userMessageQuery.MessageTitle = "";
                userMessageQuery.SystemCreated = "='N'";
                userMessageQuery.IsRead = "";
                userMessageQuery.CreatedDate = "";
                userMessageQuery.ToUserID = "";
                userMessageQuery.FromUserID = "='" + base.UserInfo.UserID + "'";
                userMessageQuery.IsActive = "='N'";
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            userMessageQuery.IsGetAll = false;
            userMessageQuery.CurrentPage = "0";
            userMessageQuery.PageSize = this.gvUserMessage.PageSize.ToString();
            userMessageQuery.SortField = "IS_ACTIVE ASC,IS_READ ASC,CREATED_DATE ";
            userMessageQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvUserMessage.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvUserMessage.Columns[1].Visible = false;
                this.gvUserMessage.Columns[2].Visible = true;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetUserMessageQueryEntity(userMessageQuery);
            this.BindData2RoleList(userMessageQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetUserMessageQueryEntity(UserMessageQueryEntity userMessageQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = userMessageQuery;
            }
            else
            {
                base.PageQueryEntitySession = userMessageQuery;
            }
        }

        public SendMessageQuery PreviousPage
        {
            get
            {
                return (SendMessageQuery) base.PreviousPage;
            }
        }
    }
}

